package fr.utbm.gi.vi51.slizers.pw.gui.menu;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.ref.WeakReference;
import java.util.EventListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import fr.utbm.gi.vi51.slizers.pw.gui.LaunchSimulationListener;

/**
 * @author Adrien
 * 
 * Implementation of a GUI Panel permitting to select a display some controls for the {@link MenuPanel}
 *
 */
public class MenuButtonPanel extends JPanel
{
	/**
	 * Version number associated to the serializable class.
	 */
	private static final long 	serialVersionUID = -3389614984647054289L;
	
	/**
	 * The button used to launch the simulation.
	 */
	private final JButton 		lauchSimulationButton;
	
	/**
	 * The combo box to select the number of creatures to launch
	 */
	private JComboBox			creaturesNbCB;
	
	/**
	 * The listener on launch simulation button press.
	 */
	private final WeakReference< LaunchSimulationListener >	gui;
	
	public MenuButtonPanel( LaunchSimulationListener _gui )
	{
		this.gui = new WeakReference< LaunchSimulationListener >( _gui );
		
		//Borders
		Border border = BorderFactory.createLineBorder( Color.BLACK );
		setBorder( border );
		
		// Display a label for the creaturesNbCB
		JLabel cbLabel = new JLabel("Number of creatures: ");
		this.add(  cbLabel );
		
		//Set up the creaturesNbCB
		Integer[] elements = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15,
				20, 25, 30, 40, 50, 60, 70, 80, 90, 100, 200, 1000};
		this.creaturesNbCB = new JComboBox( elements );
		this.add( this.creaturesNbCB );
		
		// Set up the launch simulation button
		this.lauchSimulationButton = new JButton( "Launch Simulation" ); //$NON-NLS-1$

		// Set up the listener
		this.lauchSimulationButton.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				MenuButtonPanel.this.gui.get().launchSimulationPressed();
			}
		} );
		
		// Add the button component to the JPanel
		this.add( this.lauchSimulationButton );
	}
	
	/**
	 * Returns the number of creatures set by the user
	 * 
	 * @return the number of creatures set by the user
	 */
	public int getCreaturesNumber()
	{
		return ( Integer ) this.creaturesNbCB.getSelectedItem();
	}
	
	/**
	 * Set the creaturesNbCB enable state
	 */
	public void setCBState( boolean _enable )
	{
		this.creaturesNbCB.setEnabled( _enable );
		
	}
}
